package com.hepinghospital.user.ui.activity.nursing;

import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.eventbus.Logout;
import com.hepinghospital.user.ui.activity.nursing.fragment.HomeNurseFragment;
import com.hepinghospital.user.ui.activity.nursing.fragment.NurseConsultFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import butterknife.BindView;
import jz.joyoung.robot.util.UIConstants;

/**
 * @Description 我的护理
 * @Author WuJianFeng
 * @Date 2022/8/9 17:00
 */
public class MyNursingOrderActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;

    @Override
    protected void initViews() {
        tvTitle.setText(R.string.string_my_nursing_title);
        mViewPager.setAdapter(new NurseOrdertAdapter(this, getSupportFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);


        Intent intent = getIntent();
        boolean jumpToConsult = intent.getBooleanExtra(UIConstants.JUMP_TO_CONSULT, false);
        if (jumpToConsult) {
            mViewPager.setCurrentItem(1);
            Objects.requireNonNull(mTabLayout.getTabAt(1)).select();
        } else {
            mViewPager.setCurrentItem(0);
            Objects.requireNonNull(mTabLayout.getTabAt(0)).select();
        }

    }


    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_nursing_my_order;
    }

    static class NurseOrdertAdapter extends FragmentPagerAdapter {

        private static final int PAGE_COUNT = 2;
        private Context context;

        NurseOrdertAdapter(Context context, FragmentManager fm) {
            super(fm);
            this.context = context;
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new HomeNurseFragment();
                default:
                    return new NurseConsultFragment();
            }
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return context.getResources().getString(R.string.string_home_nurse);
            }
            return context.getResources().getString(R.string.string_nurse_consult);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(Logout event) {
        if (event.logout == 4) {
            mTabLayout.post(() -> {
                mViewPager.setCurrentItem(1);
                mTabLayout.getTabAt(1).select();
            });
        }
    }
}
